<?php
class UserTypesController extends AppController {

	var $name = 'UserTypes';

    function beforeFilter(){ 
        $this->__validateLoginStatus();
       $accessArray = $this->Session->read('accessArray');
        if (!$accessArray['AdminAccess']) {
        	$this->Session->setFlash('You don\'t have the privilage to access this page');
        	$this->redirect(array('controller' => HOME_CONTROLLER, 'action' => 'index'));
        }
     }

    function __validateLoginStatus(){ 
       if($this->Session->check('User') == false){
          $this->Session->setFlash('you must be logged in to access this page');
          $this->redirect(array('controller'=>'users','action'=>'login'));
       }
    }


	function index() {
		$this->UserType->recursive = 0;
		$this->set('userTypes', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid UserType', true));
			$this->redirect(array('action' => 'index'));
		}
		$user_type = $this->UserType->find('first', array(
											'conditions' => array(
												'UserType.id' => $id
											),
											'recursive' => -1
										)
									);
		if(!$user_type){
			$this->Session->setFlash(__('Invalid user type',true));
			$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
		}
		$this->set('userType', $user_type);
	}

	function add() {
		if (!empty($this->data)) {
			$this->UserType->create();
			if ($this->UserType->save($this->data)) {
				$this->Session->setFlash(__('The UserType has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The UserType could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid UserType', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->UserType->save($this->data)) {
				$this->Session->setFlash(__('The UserType has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The UserType could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$user_type = $this->UserType->read(null,$id);
			if(!$user_type){
				$this->Session->setFlash(__('Invalid user type',true));
				$this->redirect(array('controller'=>HOME_CONTROLLER,'action'=>'index'));
			}
			$this->data = $this->UserType->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for UserType', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->UserType->del($id)) {
			$this->Session->setFlash(__('UserType deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The UserType could not be deleted. Please, try again.', true));
		$this->redirect(array('controller'=>HOME_CONTROLLER,'action' => 'index'));
	}

}
?>
